package com.gz.purchase.library.pojo;

import android.util.Log;

import com.gz.purchase.library.util.Constants;
import com.gz.purchase.library.util.CrashHandler;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 移库入库确认界面的每个入库单实体
 * author wanner1991
 */
public class MoveInStorageItemInfo {
    private static String TAG = MoveInStorageItemInfo.class.getSimpleName();
    private int id ; //记录id
    private int storage_from_id; //移库发起方id
    private String storage_from_name; //移库发起方名称
    private List<StorageCheckProductInfo> productInfoList; //商品信息
    private Long create_at;
    private String storage_type;// 入库类型（入库历史查询界面使用）

    public String getStorage_type() {
        return storage_type;
    }

    public void setStorage_type(String storage_type) {
        this.storage_type = storage_type;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Long getCreate_at() {
        return create_at;
    }

    public void setCreate_at(Long create_at) {
        this.create_at = create_at;
    }

    public int getStorage_from_id() {
        return storage_from_id;
    }

    public void setStorage_from_id(int storage_from_id) {
        this.storage_from_id = storage_from_id;
    }

    public String getStorage_from_name() {
        return storage_from_name;
    }

    public void setStorage_from_name(String storage_from_name) {
        this.storage_from_name = storage_from_name;
    }

    public List<StorageCheckProductInfo> getProductInfoList() {
        return productInfoList;
    }

    public void setProductInfoList(List<StorageCheckProductInfo> productInfoList) {
        this.productInfoList = productInfoList;
    }

    public static List<MoveInStorageItemInfo> parse(JSONArray array) throws JSONException {
        if (array == null || array.length() == 0){
            return null;
        }
        List<MoveInStorageItemInfo> list = new ArrayList<MoveInStorageItemInfo>();
        JSONObject object ;
        MoveInStorageItemInfo moveInfo ;
        for (int i = 0 ; i < array.length() ; i ++ ){
            object = array.getJSONObject(i);
            moveInfo = new MoveInStorageItemInfo();
            moveInfo.id = object.getInt("id");
            moveInfo.storage_from_id = object.getInt("storage_from_id");
            moveInfo.storage_from_name = object.getString("storage_from_name");
            moveInfo.create_at = object.getLong("create_at");
            moveInfo.productInfoList = StorageCheckProductInfo.parse(object.getJSONArray("products"));

            list.add(moveInfo);
        }
        return  list;
    }

    /**
     * 入库历史查询界面入库单实体的解析，只有调拨入库 storage_from_id 和 storage_from_name 才不为空
     * @param array
     * @return
     * @throws JSONException
     */
    public static List<MoveInStorageItemInfo> parse2(JSONArray array) throws JSONException {
        if (array == null || array.length() == 0){
            return null;
        }
        List<MoveInStorageItemInfo> list = new ArrayList<MoveInStorageItemInfo>();
        MoveInStorageItemInfo moveInfo ;
        JSONObject object ;
        for (int i = 0 ; i < array.length() ; i++){
            object = array.getJSONObject(i);
            moveInfo = new MoveInStorageItemInfo();
            moveInfo.storage_type = object.getString("storage_type");
            if (Constants.RADIO_BUTTOM_DBRK.equals(moveInfo.storage_type)){ // 只有调拨入库 storage_from_id 和 storage_from_name 才不为空
                moveInfo.storage_from_id = object.getInt("storage_from_id");
                moveInfo.storage_from_name = object.getString("storage_from_name");
            }else{
                moveInfo.storage_from_name = moveInfo.storage_type;// 其他入库方式
            }
            moveInfo.create_at = object.getLong("create_at");
            moveInfo.productInfoList = StorageCheckProductInfo.parse4(object.getJSONArray("products"));
            list.add(moveInfo);

        }

        return list;
    }

    /**
     * 出库历史记录界面实体解析使用该方法，只有调拨出库 storage_to_id 和 storage_to_name 才不为空
     * @param array
     * @return
     * @throws JSONException
     */
    public static List<MoveInStorageItemInfo> parse3(JSONArray array) throws JSONException {
        if (array == null || array.length() == 0){
            return null;
        }
        List<MoveInStorageItemInfo> list = new ArrayList<MoveInStorageItemInfo>();
        JSONObject object ;
        MoveInStorageItemInfo moveInfo ;
        for (int i = 0 ; i < array.length() ; i ++ ){
            object = array.getJSONObject(i);
            moveInfo = new MoveInStorageItemInfo();
            moveInfo.storage_type = object.getString("storage_type");
            if(moveInfo.storage_type.equals(Constants.OUT_STORAGE_TYPE_DBCK)){//如果是调拨出库，storage_to_id 和 storage_to_name 不为空
                moveInfo.storage_from_id = object.getInt("storage_to_id"); //移库收货方id
                moveInfo.storage_from_name = object.getString("storage_to_name"); //移库收货方名称
            }else{
                moveInfo.storage_from_name = moveInfo.storage_type; //移库收货方名称

            }

            moveInfo.create_at = object.getLong("create_at");
            moveInfo.productInfoList = StorageCheckProductInfo.parse(object.getJSONArray("products"));

            list.add(moveInfo);
        }
        return  list;

    }
}
